/*
 * @Author: 470140053@qq.com 470140053@qq.com
 * @Date: 2023-04-20 17:06:41
 * @LastEditors: 470140053@qq.com 470140053@qq.com
 * @LastEditTime: 2023-05-07 01:53:35
 * @FilePath: /rpc_web/public/adminstatic/js/utils.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * 时间戳转换为时间格式
 * @param {*} timestamp 时间戳
 * @param {*} format  时间格式
 */
function format(timestamp,format="Y-m-d H:i:s") {
    if(!Number(timestamp) ) {
        return '--';
    }
    if(String(timestamp).length === 10) {
        timestamp *= 1000;
    }
    var date = new Date(timestamp);
    var y = date.getFullYear();
    var m =(date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
    var d = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
    var h =  (date.getHours() < 10 ? "0" + date.getHours() : date.getHours());
    var i = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());
    var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

    var ts = format.toLowerCase();
    ts = ts.replace('y',y);
    ts = ts.replace('m',m);
    ts = ts.replace('d',d);
    ts = ts.replace('h',h);
    ts = ts.replace('i',i);
    ts = ts.replace('s',s);
    return ts;
}

async function fileParse(file, type = "base64") {
    return new Promise(resolve => {
        let fileRead = new FileReader();
        if (type === "base64") {
            fileRead.readAsDataURL(file);
        } else if (type === "buffer") {
            fileRead.readAsArrayBuffer(file);
        }
        fileRead.onload = (ev) => {
            resolve(ev.target.result);
        };
    });
}


/**
 * 首字母大写
 * @param {*} name 
 */
function capitalized(name) {
    const capitalizedFirst = name[0].toUpperCase();
    const rest = name.slice(1);
    return capitalizedFirst + rest;
}